package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import qbfiles.QbWriter;
import res.StringVars;

public class SaveProgress extends JDialog implements ActionListener{

  private static final long serialVersionUID = 1L;
  private JPanel jContentPane = null;
  private JProgressBar jSaveProgressBar = null;
  private JLabel jSaveProgressLabel = null;
  private JPanel jButtonPanel = null;
  private JButton jCancelButton = null;
  private JPanel jProgressPanel = null;
  private QbWriter writer;
  private static final String CANCEL_ACTION = "cancel";

  /**
   * @param owner
   */
  public SaveProgress(Frame owner, QbWriter writer) {
    super(owner);
    initialize();
    this.writer = writer;
    writer.setProgressBar(getJSaveProgressBar());
    writer.setOwner(this);
  }
  
  public void execute(){
    Thread worker = new Thread(writer);
    worker.start();
    setVisible(true);
  }

  /**
   * This method initializes this
   * 
   * @return void
   */
  private void initialize() {
    this.setSize(454, 131);
    setLocationRelativeTo(null);
    this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icons/save.png")));
    
    this.setModal(true);
    this.setResizable(false);
    this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    this.setTitle(StringVars.SAVE_PROGRESS_DIALOG_TITLE);
    this.setContentPane(getJContentPane());
  }

  /**
   * This method initializes jContentPane
   * 
   * @return javax.swing.JPanel
   */
  private JPanel getJContentPane() {
    if (jContentPane == null) {
      jSaveProgressLabel = new JLabel();
      jSaveProgressLabel.setText(StringVars.SAVE_PROGRESS_DIALOG_TEXT);
      jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout(5, 5));
      jContentPane.add(getJButtonPanel(), BorderLayout.SOUTH);
      jContentPane.add(getJProgressPanel(), BorderLayout.CENTER);
    }
    return jContentPane;
  }

  /**
   * This method initializes jSaveProgressBar	
   * 	
   * @return javax.swing.JProgressBar	
   */
  private JProgressBar getJSaveProgressBar() {
    if (jSaveProgressBar == null) {
      jSaveProgressBar = new JProgressBar();
    }
    return jSaveProgressBar;
  }

  /**
   * This method initializes jButtonPanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getJButtonPanel() {
    if (jButtonPanel == null) {
      jButtonPanel = new JPanel();
      jButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
      jButtonPanel.add(getJCancelButton());
    }
    return jButtonPanel;
  }

  /**
   * This method initializes jCancelButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getJCancelButton() {
    if (jCancelButton == null) {
      jCancelButton = new JButton();
      jCancelButton.setText(StringVars.SAVE_PROGRESS_DIALOG_CANCEL);
      jCancelButton.addActionListener(this);
      jCancelButton.setActionCommand(CANCEL_ACTION);
    }
    return jCancelButton;
  }

  /**
   * This method initializes jProgressPanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getJProgressPanel() {
    if (jProgressPanel == null) {
      jProgressPanel = new JPanel();
      jProgressPanel.setLayout(new BorderLayout(5, 5));
      jProgressPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
      jProgressPanel.add(getJSaveProgressBar(), BorderLayout.CENTER);
      jProgressPanel.add(jSaveProgressLabel, BorderLayout.NORTH);
    }
    return jProgressPanel;
  }

  public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals(CANCEL_ACTION)){
      writer.cancel();
    }
  }

}  //  @jve:decl-index=0:visual-constraint="65,76"
